home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWArcShp.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  7.4 KB  |  257 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWArcShp.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWARCSHP_H
  13. #include "FWArcShp.h"
  14. #endif
  15.  
  16. #ifndef FWOVLSHP_H
  17. #include "FWOvlShp.h"
  18. #endif
  19.  
  20. #ifndef SLRENDER_H
  21. #include "SLRender.h"
  22. #endif
  23.  
  24. // ----- Foundation Includes -----
  25.  
  26. #ifndef FWSTREAM_H
  27. #include "FWStream.h"
  28. #endif
  29.  
  30. // ----- C Includes -----
  31.  
  32. #if defined(FW_BUILD_MAC) & !defined(__FP__)
  33. #include <FP.h>
  34. #endif
  35.  
  36. #ifdef FW_BUILD_WIN
  37. #include <math.h>
  38. #endif
  39.  
  40. //========================================================================================
  41. // File scope definitions
  42. //========================================================================================
  43.  
  44. #ifdef FW_BUILD_MAC
  45. #pragma segment fwgraphxshape
  46. #endif
  47.  
  48. //========================================================================================
  49. //    class FW_CArcShape
  50. //========================================================================================
  51.  
  52. FW_DEFINE_AUTO(FW_CArcShape)
  53. FW_DEFINE_CLASS_M1(FW_CArcShape, FW_CBoundedShape)
  54.  
  55. // This class is archivable, but we provide the archiving implementation in a separate
  56. // translation unit in order to enable deadstripping of the archiving-related code
  57. // in parts that do not use archiving with this class.
  58.  
  59. //----------------------------------------------------------------------------------------
  60. //    FW_CArcShape::FW_CArcShape
  61. //----------------------------------------------------------------------------------------
  62.  
  63. FW_CArcShape::FW_CArcShape(const FW_CArcShape& other) :
  64.     FW_CBoundedShape(other),
  65.     fStartAngle(other.fStartAngle),
  66.     fArcAngle(other.fArcAngle)
  67. {
  68.     FW_END_CONSTRUCTOR
  69. }
  70.  
  71. //----------------------------------------------------------------------------------------
  72. //    FW_CArcShape::FW_CArcShape
  73. //----------------------------------------------------------------------------------------
  74.  
  75. FW_CArcShape::FW_CArcShape(const FW_CRect& rect, 
  76.                             short startAngle,
  77.                             short arcAngle,
  78.                             FW_ERenderVerbs renderVerb,
  79.                             const FW_CInk& ink,
  80.                             const FW_CStyle& style) :
  81.     FW_CBoundedShape(rect, renderVerb, ink, style, FW_kNormalFont),
  82.     fStartAngle(startAngle),
  83.     fArcAngle(arcAngle)
  84. {
  85.     FW_END_CONSTRUCTOR
  86. }
  87.  
  88. //----------------------------------------------------------------------------------------
  89. //    FW_CArcShape::FW_CArcShape
  90. //----------------------------------------------------------------------------------------
  91.  
  92. FW_CArcShape::FW_CArcShape(FW_CReadableStream& stream) :
  93.     FW_CBoundedShape(stream)
  94. {
  95.     stream >> fStartAngle;
  96.     stream >> fArcAngle;
  97.  
  98.     FW_END_CONSTRUCTOR
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------
  102. //    FW_CArcShape::~FW_CArcShape
  103. //----------------------------------------------------------------------------------------
  104.  
  105. FW_CArcShape::~FW_CArcShape()
  106. {
  107.     FW_START_DESTRUCTOR
  108. }
  109.  
  110. //----------------------------------------------------------------------------------------
  111. //    FW_CArcShape::operator=
  112. //----------------------------------------------------------------------------------------
  113.  
  114. FW_CArcShape& FW_CArcShape::operator=(const FW_CArcShape& other)
  115. {
  116.     if (this != &other)
  117.     {
  118.         FW_CBoundedShape::operator=(other);
  119.     
  120.         fStartAngle = other.fStartAngle;
  121.         fArcAngle = other.fArcAngle;
  122.     }
  123.     
  124.     return *this;
  125. }
  126.  
  127. //----------------------------------------------------------------------------------------
  128. //    FW_CArcShape::Render
  129. //----------------------------------------------------------------------------------------
  130.  
  131. void FW_CArcShape::Render(FW_CGraphicContext& gc) const
  132. {
  133.     FW_PrivRenderArc(gc.GetEnvironment(),
  134.         gc,
  135.         fRect,
  136.         fStartAngle, fArcAngle,
  137.         GetRenderVerb(),
  138.         fInk,
  139.         fStyle);
  140.     FW_FailOnEvError(gc.GetEnvironment());
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. //    FW_CArcShape::RenderArc
  145. //----------------------------------------------------------------------------------------
  146.  
  147. void FW_CArcShape::RenderArc(FW_CGraphicContext& gc,
  148.                                 const FW_CRect& rect, 
  149.                                 short startAngle, short arcAngle,
  150.                                 FW_ERenderVerbs renderVerb, 
  151.                                 const FW_CInk& ink, 
  152.                                 const FW_CStyle& style)
  153. {    
  154.     FW_PrivRenderArc(gc.GetEnvironment(),
  155.         gc,
  156.         rect,
  157.         startAngle, arcAngle,
  158.         renderVerb,
  159.         ink,
  160.         style);
  161.     FW_FailOnEvError(gc.GetEnvironment());
  162. }
  163.  
  164. //----------------------------------------------------------------------------------------
  165. //    FW_CArcShape::CalcAngle
  166. //----------------------------------------------------------------------------------------
  167.  
  168. short FW_CArcShape::CalcAngle(const FW_CPoint& test) const
  169. {
  170.     FW_Double width  = FW_FixedToDouble(fRect.right - fRect.left);
  171.     FW_Double height = FW_FixedToDouble(fRect.bottom - fRect.top);
  172.     
  173.     FW_CPoint testPoint(FW_Half(test.x + test.x - fRect.right - fRect.left), FW_Half(test.y + test.y - fRect.bottom - fRect.top));
  174.     
  175.     if (testPoint.y == FW_kFixed0 || height == 0)
  176.         return testPoint.x < FW_kFixed0 ? 270 : 90;
  177.  
  178.     if (testPoint.x == FW_kFixed0 || width == 0)
  179.         return testPoint.y < FW_kFixed0 ? 180 : 0;
  180.  
  181.     FW_Double arc = atan((FW_FixedToDouble(testPoint.y) * height) / (FW_FixedToDouble(testPoint.x) * width));
  182.     
  183.     arc = ((180.0 * arc) / 3.1415926536) + .5;
  184.     short shortArc = (short)arc + 90;
  185.     if (testPoint.x < FW_kFixed0)
  186.         shortArc += 180;
  187.                 
  188.     return shortArc;
  189. }
  190.  
  191. //----------------------------------------------------------------------------------------
  192. //    FW_CArcShape::HitTest
  193. //----------------------------------------------------------------------------------------
  194.  
  195. FW_Boolean FW_CArcShape::HitTest(FW_CGraphicContext& gc,
  196.                                  const FW_CPoint& test,
  197.                                  FW_Fixed tolerance) const
  198. {
  199.     if(fRenderVerb == FW_kNoRendering)
  200.         return FALSE;
  201.  
  202.     if (FW_CBoundedShape::HitTest(gc, test, tolerance))
  203.     {
  204.         short arcAngle = CalcAngle(test) - fStartAngle;
  205.         if (((arcAngle & 0x8000) == (fArcAngle & 0x8000)) &&    // same sign
  206.             FW_Absolute((long)fArcAngle) >= FW_Absolute((long)arcAngle))     
  207.         {
  208.             FW_COvalShape oval(fRect, fRenderVerb);
  209.             return oval.HitTest(gc, test, tolerance);
  210.         }
  211.     }
  212.  
  213.     return FALSE;
  214. }
  215.  
  216. //----------------------------------------------------------------------------------------
  217. //    FW_CArcShape::Copy
  218. //----------------------------------------------------------------------------------------
  219.  
  220. FW_CShape* FW_CArcShape::Copy() const
  221. {
  222.     return FW_NEW(FW_CArcShape, (*this));
  223. }
  224.  
  225. //----------------------------------------------------------------------------------------
  226. //    FW_CArcShape::GetGeometry
  227. //----------------------------------------------------------------------------------------
  228.  
  229. void FW_CArcShape::GetGeometry(FW_CRect& rect, short& startAngle, short& arcAngle) const
  230. {
  231.     rect = fRect;
  232.     startAngle = fStartAngle;
  233.     arcAngle = fArcAngle;
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. //    FW_CArcShape::SetGeometry
  238. //----------------------------------------------------------------------------------------
  239.  
  240. void FW_CArcShape::SetGeometry(const FW_CRect& rect, short startAngle, short arcAngle)
  241. {
  242.     fRect = rect;
  243.     fStartAngle = startAngle;
  244.     fArcAngle = arcAngle;
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. //    FW_CArcShape::Flatten
  249. //----------------------------------------------------------------------------------------
  250.  
  251. void FW_CArcShape::Flatten(FW_CWritableStream& stream) const
  252. {
  253.     FW_CBoundedShape::Flatten(stream);
  254.     stream << fStartAngle;
  255.     stream << fArcAngle;
  256. }
  257.